/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.wx.common;

import cn.ac.iie.di.wx.configuration.Configuration;
import cn.ac.iie.di.wx.mongoservice.FansMongoThread;
import cn.ac.iie.di.wx.mongoservice.MongoHelper;
import cn.ac.iie.di.wx.vo.FansUpdateVo;
import cn.ac.iie.di.wx.vo.ReadCntVo;
import cn.ac.iie.di.wx.vo.RealIDQueryVo;
import cn.ac.iie.di.wx.vo.PicUrlVo;
import cn.ac.iie.di.wx.vo.ReadCntMqVo;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.log4j.Logger;

/**
 *
 * @author AlexMu
 */
public class RuntimeEnv {

    private static Configuration conf = null;
    public static final String JETTY_SERVER = "jettyServer";
    public static final String JETTY_PORT = "jettyPort";
    public static final String JETTY_THREADNUM = "jettyThreadNum";
    public static final String LIMITINT = "limitint";
    public static final String TXURL = "txUrl";

    public static final String WXMSGNEWMONGOHOSTS = "msg_new_hosts";
    public static final String WXMSGNEWDBNAME = "msg_new_dbname";
    public static final String WXMSGNEWTABLENAME_GROUP = "msg_new_tablename_group";
    public static final String WXMSGNEWTABLENAME_INTERACTIVE = "msg_new_tablename_interactive";
    public static final String WXMSGNEWUSERNAME = "msg_new_username";
    public static final String WXMSGNEWPASSWORD = "msg_new_passwd";

    public static final String WXMSGOLDMONGOHOSTS = "msg_old_hosts";
    public static final String WXMSGOLDDBNAME = "msg_old_dbname";
    public static final String WXMSGOLDTABLENAME = "msg_old_tablename";
    public static final String WXMSGOLDUSERNAME = "msg_old_username";
    public static final String WXMSGOLDPASSWORD = "msg_old_passwd";

    public static final String WXUSERNEWMONGOHOSTS = "user_new_hosts";
    public static final String WXUSERNEWDBNAME = "user_new_dbname";
    public static final String WXUSERNEWTABLENAME = "user_new_tablename";
    public static final String WXUSERNEWUSERNAME = "user_new_username";
    public static final String WXUSERNEWPASSWORD = "user_new_passwd";

    public static final String WXUSEROLDMONGOHOSTS = "user_old_hosts";
    public static final String WXUSEROLDDBNAME = "user_old_dbname";
    public static final String WXUSEROLDTABLENAME = "user_old_tablename";
    public static final String WXUSEROLDUSERNAME = "user_old_username";
    public static final String WXUSEROLDPASSWORD = "user_old_passwd";

    public static final String PICMONGOHOSTS = "picHosts";
    public static final String PICDBNAME = "picDbname";
    public static final String PICTABLENAME = "picTablename";
    public static final String PICUSERNAME = "picUsername";
    public static final String PICPASSWORD = "picPassword";

    public static final String MONGOCONNECTLIMIT = "mongoConnectLimit";
    public static final String WXFANSCNTQLIMIT = "wxFansCntQueryLimit";
    public static final String WXPICQLIMIT = "wxPictureUrlQueryLimit";
    public static final String WXREADCNTLIMIT = "wxReadCntLimit";
    public static final String WXREALIDQLIMIT = "wxRealIdQueryLimit";
    public static final String CORETHREADSIZE = "coreThreadSize";
    public static final String MAXTHREADNUM = "maxThreadNum";

    public static final String REDIS_MASTER_NAME = "redis.master.name";
    public static final String REDIS_HOSTS = "redis.hosts";
    public static final String REDIS_DB_NAME = "redis.db.name";
    public static final String ICTURL = "ictUrl";
    public static final String RECONNECTION = "REConnection";
    public static final String RETOPIC = "REtopic";
    public static final String COLUMN = "column";
    public static final String WTPATH = "writeToFilePath";
    public static final String FNAME = "fileName";
    public static final String READCNTFNAME = "readCntFileName";
    public static final String PICCACHE = "piccache";
    public static final String cacheFlag = "cacheFlag";
    public static final String wxFansCntUrl = "wxFansCntUrl";
    public static final String wxReadCntUrl = "wxReadCntUrl";
    public static final String wxRealIdQueryUrl = "wxRealIdQueryUrl";
    public static final String wxPicQueryUrl = "wxPicQueryUrl";
    public static final String wxPicQueryUrl_new = "wxPicQueryUrl_new";

//添加消息队列的相关参数，进行消息队列测试
    public static AtomicInteger wxFansCntQueryLimit = new AtomicInteger(0);
    public static AtomicInteger wxPictureUrlQueryLimit = new AtomicInteger(0);
    public static AtomicInteger wxReadCntLimit = new AtomicInteger(0);
    public static AtomicInteger wxRealIdQueryLimit = new AtomicInteger(0);

    public static AtomicInteger wxFansCntQueryCount = new AtomicInteger(0);
    public static AtomicInteger wxPictureUrlQueryCount = new AtomicInteger(0);
    public static AtomicInteger wxReadCntCount = new AtomicInteger(0);
    public static AtomicInteger wxRealIdQueryCount = new AtomicInteger(0);
    private static Map<String, Object> dynamicParams = new HashMap<String, Object>();
    static Logger logger = Logger.getLogger(RuntimeEnv.class.getName());
    public static FansMongoThread thread;//入库的线程不止一个，按照入不同的库区分，确定以后再创建一个
    public static LinkedBlockingQueue<FansUpdateVo> fansWxUserOldBlockingQueue = new LinkedBlockingQueue(200);//用于接收各个handler的返回值，以便mongo入库线程处理入库，数量根据具体入库线程和键值确定
    public static LinkedBlockingQueue<FansUpdateVo> fansWxUserNewBlockingQueue = new LinkedBlockingQueue(200);//用于接收各个handler的返回值，以便mongo入库线程处理入库，数量根据具体入库线程和键值确定
    public static LinkedBlockingQueue<ReadCntVo> readCntWxmsgGBlockingQueue = new LinkedBlockingQueue(200);//用于接收各个handler的返回值，以便mongo入库线程处理入库，数量根据具体入库线程和键值确定
    public static LinkedBlockingQueue<ReadCntVo> readCntWxmsgIBlockingQueue = new LinkedBlockingQueue(200);//用于接收各个handler的返回值，以便mongo入库线程处理入库，数量根据具体入库线程和键值确定
    public static LinkedBlockingQueue<ReadCntVo> readCntDocinfoBlockingQueue = new LinkedBlockingQueue(200);//用于接收各个handler的返回值，以便mongo入库线程处理入库，数量根据具体入库线程和键值确定
    public static LinkedBlockingQueue<RealIDQueryVo> realIdWxUserOldBlockingQueue = new LinkedBlockingQueue(200);//用于接收各个handler的返回值，以便mongo入库线程处理入库，数量根据具体入库线程和键值确定
    public static LinkedBlockingQueue<RealIDQueryVo> realIdWxUserNewBlockingQueue = new LinkedBlockingQueue(200);//用于接收各个handler的返回值，以便mongo入库线程处理入库，数量根据具体入库线程和键值确定
    public static LinkedBlockingQueue<PicUrlVo> picUrlBlockingQueue = new LinkedBlockingQueue(200);//用于接收各个handler的返回值，以便mongo入库线程处理入库，数量根据具体入库线程和键值确定
    public static LinkedBlockingQueue<ReadCntMqVo> readCntMqBlockingQueue = new LinkedBlockingQueue(200);//用于接收各个handler的返回值，以便mongo入库线程处理入库，数量根据具体入库线程和键值确定

    public static boolean initialize(Configuration pConf) throws Exception {

        if (pConf == null) {
            logger.error("configuration object is null");
            throw new Exception("configuration object is null");
        }

        conf = pConf;

        String jettyPort = conf.getString(JETTY_PORT, "");
        if (jettyPort.isEmpty()) {
            logger.error("parameter jettyPort does not exist or is not defined");
            throw new Exception("parameter jettyPort does not exist or is not defined");
        }
        addParam(JETTY_PORT, jettyPort);

        String jettyServer = conf.getString(JETTY_SERVER, "");
        if (jettyServer.isEmpty()) {
            logger.error("parameter jettyServer does not exist or is not defined");
            throw new Exception("parameter jettyServer does not exist or is not defined");
        }
        addParam(JETTY_SERVER, jettyServer);

        String jettyThreadNum = conf.getString(JETTY_THREADNUM, "");
        if (jettyThreadNum.isEmpty()) {
            logger.error("parameter jettyThreadNum does not exist or is not defined");
            throw new Exception("parameter jettyThreadNum does not exist or is not defined");
        }
        addParam(JETTY_THREADNUM, jettyThreadNum);

        String limitint = conf.getString(LIMITINT, "");
        if (limitint.isEmpty()) {
            logger.error("parameter limitint does not exist or is not defined");
            throw new Exception("parameter limitint does not exist or is not defined");
        }
        addParam(LIMITINT, limitint);

        String txUrl = conf.getString(TXURL, "");
        if (txUrl.isEmpty()) {
            logger.error("parameter txUrl does not exist or is not defined");
            throw new Exception("parameter txUrl does not exist or is not defined");
        }
        addParam(TXURL, txUrl);

        String msg_new_hosts = conf.getString(WXMSGNEWMONGOHOSTS, "");
        if (msg_new_hosts.isEmpty()) {
            logger.error("parameter msg_new_hosts does not exist or is not defined");
            throw new Exception("parameter msg_new_hosts does not exist or is not defined");
        }
        addParam(WXMSGNEWMONGOHOSTS, msg_new_hosts);

        String msg_new_passwd = conf.getString(WXMSGNEWPASSWORD, "");
        if (msg_new_passwd.isEmpty()) {
            logger.error("parameter msg_new_passwd does not exist or is not defined");
            throw new Exception("parameter msg_new_passwd does not exist or is not defined");
        }
        addParam(WXMSGNEWPASSWORD, msg_new_passwd);

        String msg_new_dbname = conf.getString(WXMSGNEWDBNAME, "");
        if (msg_new_dbname.isEmpty()) {
            logger.error("parameter msg_new_dbname does not exist or is not defined");
            throw new Exception("parameter msg_new_dbname does not exist or is not defined");
        }
        addParam(WXMSGNEWDBNAME, msg_new_dbname);

        String msg_new_tablename_group = conf.getString(WXMSGNEWTABLENAME_GROUP, "");
        if (msg_new_tablename_group.isEmpty()) {
            logger.error("parameter msg_new_tablename_group does not exist or is not defined");
            throw new Exception("parameter msg_new_tablename_group does not exist or is not defined");
        }
        addParam(WXMSGNEWTABLENAME_GROUP, msg_new_tablename_group);

        String msg_new_tablename_interactive = conf.getString(WXMSGNEWTABLENAME_INTERACTIVE, "");
        if (msg_new_tablename_interactive.isEmpty()) {
            logger.error("parameter msg_new_tablename_interactive does not exist or is not defined");
            throw new Exception("parameter msg_new_tablename_interactive does not exist or is not defined");
        }
        addParam(WXMSGNEWTABLENAME_INTERACTIVE, msg_new_tablename_interactive);

        String msg_new_username = conf.getString(WXMSGNEWUSERNAME, "");
        if (msg_new_username.isEmpty()) {
            logger.error("parameter msg_new_username does not exist or is not defined");
            throw new Exception("parameter msg_new_username does not exist or is not defined");
        }
        addParam(WXMSGNEWUSERNAME, msg_new_username);

        String msg_old_hosts = conf.getString(WXMSGOLDMONGOHOSTS, "");
        if (msg_old_hosts.isEmpty()) {
            logger.error("parameter msg_old_hosts does not exist or is not defined");
            throw new Exception("parameter msg_old_hosts does not exist or is not defined");
        }
        addParam(WXMSGOLDMONGOHOSTS, msg_old_hosts);

        String msg_old_dbname = conf.getString(WXMSGOLDDBNAME, "");
        if (msg_old_dbname.isEmpty()) {
            logger.error("parameter msg_old_dbname does not exist or is not defined");
            throw new Exception("parameter msg_old_dbname does not exist or is not defined");
        }
        addParam(WXMSGOLDDBNAME, msg_old_dbname);

        String msg_old_tablename = conf.getString(WXMSGOLDTABLENAME, "");
        if (msg_old_tablename.isEmpty()) {
            logger.error("parameter msg_old_tablename does not exist or is not defined");
            throw new Exception("parameter msg_old_tablename does not exist or is not defined");
        }
        addParam(WXMSGOLDTABLENAME, msg_old_tablename);

        String msg_old_username = conf.getString(WXMSGOLDUSERNAME, "");
        if (msg_old_username.isEmpty()) {
            logger.error("parameter msg_old_username does not exist or is not defined");
            throw new Exception("parameter msg_old_username does not exist or is not defined");
        }
        addParam(WXMSGOLDUSERNAME, msg_old_username);

        String msg_old_passwd = conf.getString(WXMSGOLDPASSWORD, "");
        if (msg_old_passwd.isEmpty()) {
            logger.error("parameter msg_old_passwd does not exist or is not defined");
            throw new Exception("parameter msg_old_passwd does not exist or is not defined");
        }
        addParam(WXMSGOLDPASSWORD, msg_old_passwd);

        String user_new_hosts = conf.getString(WXUSERNEWMONGOHOSTS, "");
        if (user_new_hosts.isEmpty()) {
            logger.error("parameter user_new_hosts does not exist or is not defined");
            throw new Exception("parameter user_new_hosts does not exist or is not defined");
        }
        addParam(WXUSERNEWMONGOHOSTS, user_new_hosts);

        String user_new_dbname = conf.getString(WXUSERNEWDBNAME, "");
        if (user_new_dbname.isEmpty()) {
            logger.error("parameter user_new_dbname does not exist or is not defined");
            throw new Exception("parameter user_new_dbname does not exist or is not defined");
        }
        addParam(WXUSERNEWDBNAME, user_new_dbname);

        String user_new_tablename = conf.getString(WXUSERNEWTABLENAME, "");
        if (user_new_tablename.isEmpty()) {
            logger.error("parameter user_new_tablename does not exist or is not defined");
            throw new Exception("parameter user_new_tablename does not exist or is not defined");
        }
        addParam(WXUSERNEWTABLENAME, user_new_tablename);

        String user_new_username = conf.getString(WXUSERNEWUSERNAME, "");
        if (user_new_username.isEmpty()) {
            logger.error("parameter user_new_username does not exist or is not defined");
            throw new Exception("parameter user_new_username does not exist or is not defined");
        }
        addParam(WXUSERNEWUSERNAME, user_new_username);

        String user_new_passwd = conf.getString(WXUSERNEWPASSWORD, "");
        if (user_new_passwd.isEmpty()) {
            logger.error("parameter user_new_passwd does not exist or is not defined");
            throw new Exception("parameter user_new_passwd does not exist or is not defined");
        }
        addParam(WXUSERNEWPASSWORD, user_new_passwd);

        String user_old_hosts = conf.getString(WXUSEROLDMONGOHOSTS, "");
        if (user_old_hosts.isEmpty()) {
            logger.error("parameter user_old_hosts does not exist or is not defined");
            throw new Exception("parameter user_old_hosts does not exist or is not defined");
        }
        addParam(WXUSEROLDMONGOHOSTS, user_old_hosts);

        String user_old_dbname = conf.getString(WXUSEROLDDBNAME, "");
        if (user_old_dbname.isEmpty()) {
            logger.error("parameter user_old_dbname does not exist or is not defined");
            throw new Exception("parameter user_old_dbname does not exist or is not defined");
        }
        addParam(WXUSEROLDDBNAME, user_old_dbname);

        String user_old_tablename = conf.getString(WXUSEROLDTABLENAME, "");
        if (user_old_tablename.isEmpty()) {
            logger.error("parameter user_old_tablename does not exist or is not defined");
            throw new Exception("parameter user_old_tablename does not exist or is not defined");
        }
        addParam(WXUSEROLDTABLENAME, user_old_tablename);

        String user_old_username = conf.getString(WXUSEROLDUSERNAME, "");
        if (user_old_username.isEmpty()) {
            logger.error("parameter user_old_username does not exist or is not defined");
            throw new Exception("parameter user_old_username does not exist or is not defined");
        }
        addParam(WXUSEROLDUSERNAME, user_old_username);

        String user_old_passwd = conf.getString(WXUSEROLDPASSWORD, "");
        if (user_old_passwd.isEmpty()) {
            logger.error("parameter user_old_passwd does not exist or is not defined");
            throw new Exception("parameter user_old_passwd does not exist or is not defined");
        }
        addParam(WXUSEROLDPASSWORD, user_old_passwd);

        String picHosts = conf.getString(PICMONGOHOSTS, "");
        if (picHosts.isEmpty()) {
            logger.error("parameter picHosts does not exist or is not defined");
            throw new Exception("parameter picHosts does not exist or is not defined");
        }
        addParam(PICMONGOHOSTS, picHosts);

        String picDbname = conf.getString(PICDBNAME, "");
        if (picDbname.isEmpty()) {
            logger.error("parameter picDbname does not exist or is not defined");
            throw new Exception("parameter picDbname does not exist or is not defined");
        }
        addParam(PICDBNAME, picDbname);

        String picTablename = conf.getString(PICTABLENAME, "");
        if (picTablename.isEmpty()) {
            logger.error("parameter picTablename does not exist or is not defined");
            throw new Exception("parameter picTablename does not exist or is not defined");
        }
        addParam(PICTABLENAME, picTablename);

        String picUsername = conf.getString(PICUSERNAME, "");
        if (picUsername.isEmpty()) {
            logger.error("parameter picUsername does not exist or is not defined");
            throw new Exception("parameter picUsername does not exist or is not defined");
        }
        addParam(PICUSERNAME, picUsername);

        String picPassword = conf.getString(PICPASSWORD, "");
        if (picPassword.isEmpty()) {
            logger.error("parameter picPassword does not exist or is not defined");
            throw new Exception("parameter picPassword does not exist or is not defined");
        }
        addParam(PICPASSWORD, picPassword);

        String mongoConnectLimit = conf.getString(MONGOCONNECTLIMIT, "");
        if (mongoConnectLimit.isEmpty()) {
            logger.error("parameter mongoConnectLimit does not exist or is not defined");
            throw new Exception("parameter mongoConnectLimit does not exist or is not defined");
        }
        addParam(MONGOCONNECTLIMIT, mongoConnectLimit);

        String wxFansCntQLimit = conf.getString(WXFANSCNTQLIMIT, "");
        if (wxFansCntQLimit.isEmpty()) {
            logger.error("parameter wxFansCntQueryLimit does not exist or is not defined");
            throw new Exception("parameter wxFansCntQueryLimit does not exist or is not defined");
        }
        addParam(WXFANSCNTQLIMIT, wxFansCntQLimit);

        String wxPictureUrlQLimit = conf.getString(WXPICQLIMIT, "");
        if (wxPictureUrlQLimit.isEmpty()) {
            logger.error("parameter wxPictureUrlQueryLimit does not exist or is not defined");
            throw new Exception("parameter wxPictureUrlQueryLimit does not exist or is not defined");
        }
        addParam(WXPICQLIMIT, wxPictureUrlQLimit);

        String wxReadCountLimit = conf.getString(WXREADCNTLIMIT, "");
        if (wxReadCountLimit.isEmpty()) {
            logger.error("parameter wxReadCntLimit does not exist or is not defined");
            throw new Exception("parameter wxReadCntLimit does not exist or is not defined");
        }
        addParam(WXREADCNTLIMIT, wxReadCountLimit);

        String wxRealIdQLimit = conf.getString(WXREALIDQLIMIT, "");
        if (wxRealIdQLimit.isEmpty()) {
            logger.error("parameter wxRealIdQueryLimit does not exist or is not defined");
            throw new Exception("parameter wxRealIdQueryLimit does not exist or is not defined");
        }
        addParam(WXREALIDQLIMIT, wxRealIdQLimit);

        String coreThreadSize = conf.getString(CORETHREADSIZE, "");
        if (coreThreadSize.isEmpty()) {
            logger.error("parameter coreThreadSize does not exist or is not defined");
            throw new Exception("parameter coreThreadSize does not exist or is not defined");
        }
        addParam(CORETHREADSIZE, coreThreadSize);

        String maxThreadNum = conf.getString(MAXTHREADNUM, "");
        if (maxThreadNum.isEmpty()) {
            logger.error("parameter maxThreadNum does not exist or is not defined");
            throw new Exception("parameter maxThreadNum does not exist or is not defined");
        }
        addParam(MAXTHREADNUM, maxThreadNum);

        String redis_master_name = conf.getString(REDIS_MASTER_NAME, "");
        if (REDIS_MASTER_NAME.isEmpty()) {
            logger.error("parameter redis_master_name does not exist or is not defined");
            throw new Exception("parameter redis_master_name does not exist or is not defined");
        }
        addParam(REDIS_MASTER_NAME, redis_master_name);

        String redis_hosts = conf.getString(REDIS_HOSTS, "");
        if (REDIS_HOSTS.isEmpty()) {
            logger.error("parameter redis_hosts does not exist or is not defined");
            throw new Exception("parameter redis_hosts does not exist or is not defined");
        }
        addParam(REDIS_HOSTS, redis_hosts);

        String redis_db_name = conf.getString(REDIS_DB_NAME, "");
        if (REDIS_DB_NAME.isEmpty()) {
            logger.error("parameter redis_db_name does not exist or is not defined");
            throw new Exception("parameter redis_db_name does not exist or is not defined");
        }
        addParam(REDIS_DB_NAME, redis_db_name);

        String ictUrl = conf.getString(ICTURL, "");
        if (ictUrl.isEmpty()) {
            logger.error("parameter ictUrl does not exist or is not defined");
            throw new Exception("parameter ictUrl does not exist or is not defined");
        }
        addParam(ICTURL, ictUrl);

        String REConnection = conf.getString(RECONNECTION, "");
        if (REConnection.isEmpty()) {
            logger.error("parameter REConnection does not exist or is not defined");
            throw new Exception("parameter REConnection does not exist or is not defined");
        }
        addParam(RECONNECTION, REConnection);

        String REtopic = conf.getString(RETOPIC, "");
        if (REtopic.isEmpty()) {
            logger.error("parameter REtopic does not exist or is not defined");
            throw new Exception("parameter REtopic does not exist or is not defined");
        }
        addParam(RETOPIC, REtopic);

        String column = conf.getString(COLUMN, "");
        if (column.isEmpty()) {
            logger.error("parameter column does not exist or is not defined");
            throw new Exception("parameter column does not exist or is not defined");
        }
        addParam(COLUMN, column);

        setKeyAndValue(WTPATH, "writeToFilePath");
        setKeyAndValue(FNAME, "fileName");
        setKeyAndValue(READCNTFNAME, "readCntFileName");
        setKeyAndValue(PICCACHE, "piccache");
        setKeyAndValue(cacheFlag, "cacheFlag");

        setKeyAndValue(wxFansCntUrl, "wxFansCntUrl");
        setKeyAndValue(wxReadCntUrl, "wxReadCntUrl");
        setKeyAndValue(wxRealIdQueryUrl, "wxRealIdQueryUrl");
        setKeyAndValue(wxPicQueryUrl, "wxPicQueryUrl");
        setKeyAndValue(wxPicQueryUrl_new, "wxPicQueryUrl_new");
        return true;
    }

    public static boolean setKeyAndValue(String KEY, String parameter) throws Exception {
        String value = conf.getString(KEY, "");
        if (value.isEmpty()) {
            logger.error("parameter " + parameter + " does not exist or is not defined");
            throw new Exception("parameter " + parameter + " does not exist or is not defined");
        }
        addParam(KEY, value);
        return true;
    }

    public static void dumpEnvironment() {
        conf.dumpConfiguration();
    }

    public static void addParam(String pParamName, Object pValue) {
        synchronized (dynamicParams) {
            dynamicParams.put(pParamName, pValue);
        }
    }

    public static Object getParam(String pParamName) {
        return dynamicParams.get(pParamName);
    }
}
